$(document).ready(function(){
   
   // Uses jQuery to call the Perl scrip and
   // pass it current BibID, this returns recommendations.
   // If there are recommendations, this script displays
   // them

   // Messages that are displayed to the user
   var msgStartRecommend = new Array();
   var msgYesRecommend = new Array();
   var msgTitleRecommend = new Array();
   var msgNoRecommend = new Array();
   var msgRecommendedHelpLink = new Array();
   var msgRecommendedHelp = new Array();
   msgStartRecommend["en"]="Looking for recommendations...";
   msgStartRecommend["cy"]="Yn chwilio am argymhellion...";
   msgYesRecommend["en"]="There are recommendations, click here to view them";
   msgYesRecommend["cy"]="Argymhellion ar gael, cliciwch yma i edrych arnynt";
   msgTitleRecommend["en"]="Other items you may be interested in";
   msgTitleRecommend["cy"]="Eitemau eraill sydd efallai o ddiddordeb i chi";
   msgNoRecommend["en"]="No recommendations were found";
   msgNoRecommend["cy"]="Ni chafwyd argymhellion";
   msgRecommendedHelpLink["en"]="What is this?";
   msgRecommendedHelpLink["cy"]="Beth yw hwn?";
   msgRecommendedHelp["en"]="Recommendations are made based on previous loans. If users who have borrowed this item have also frequently borrowed other items, these items will be recommended to you here.";
   msgRecommendedHelp["cy"]="Mae argymhellion yn cael eu seilio ar fenthyciadau blaenorol. Os yw defnyddwyr sydd wedi benthyca'r eitem hon hefyd yn aml wedi benthyca eitemau eraill, bydd yr eitemau hyn yn cael eu hargymhell ichi yma.";
   
   // Location of recommend.pl
   var scriptPath="/cgi-bin/recommend/recommend.pl";
   
   // Location of the progress spinner
   var spinnerPath = "/images/loading_recommend.gif";
   
   ////////////////////////////////////
   // End of the user editable stuff //
   ////////////////////////////////////
   
   
   // Get the URL of the current page
   var url=document.location.href;

   // Get the page language
   var hdcont = document.getElementsByTagName("html");
   var lang = hdcont[0].lang;
   
   // Extract the bibid
   var bibid=url.match(/bibId=\d+/g);
   bibid=String(bibid).replace(/bibId=/g,"");

   // Only display the recommendations if we've been able to determine the bib id
   if (bibid != "null") {
      $.ajax({
         type: "GET",
         url: scriptPath,
         data: "b="+bibid,
         // Display a holding message while we're in the process of
         // getting the recommendations
         beforeSend: function() {
            $("#au_recommend").html('<p style="font-weight:bold;">'+msgStartRecommend[lang]+' <img src="'+spinnerPath+'" alt="Loading icon" /></p>');
         },
         // If an error occurred (most likely the Perl script
         // failed for some reason), tell the user there were no
         // recommendations
         error: function() {
            $("#au_recommend").html('<p style="font-weight:bold;">'+msgNoRecommend[lang]+'</p>');
         },
         // Display the link to the results, if we didn't get any, display a
         // message saying so
         success: function(resp){
            if (resp.match(/recommend/g)) {
               $("#au_recommend").html('<p id="showRecommended" style="font-weight:bold;"><a href="#" id="showRecommendedLink">'+msgYesRecommend[lang]+'</a> [<a href="#" id="showRecommendedHelpLink">'+msgRecommendedHelpLink[lang]+'</a>]</p><p id="recommendedHelp" style="display:none;">'+msgRecommendedHelp[lang]+'</p>'+resp);
            } else {
               $("#au_recommend").html('<p style="font-weight:bold;">'+msgNoRecommend[lang]+'</p>');
            }
         }
      });
   }

   // Show the recommendations when the link is clicked
   $("#showRecommendedLink").live("click", function() {
      // Hide the help text (if it is being displayed
      // Then show the recommendations
      $("#recommendedHelp").slideUp(function() {
         $("#showRecommended").html(msgTitleRecommend[lang]+' [<a href="#" id="showRecommendedHelpLink">'+msgRecommendedHelpLink[lang]+'</a>]');
         $("#recommended").slideDown();
      });
      return false;
   });
   
   // Show the recommendations help text
   $("#showRecommendedHelpLink").live("click", function() {
      $("#recommendedHelp").slideDown();
      return false;
   });

});